package com.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.ProjectMapper;
import com.mapper.TypeMapper;
import com.pojo.Project;
import com.pojo.Type;
import com.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class TypeServiceImpl implements TypeService {
    @Autowired
    private TypeMapper typeMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Override
    public List<Type> selectAllType() {
        List<Type> types = typeMapper.selectAllType();
        for (int i=0;i<types.size();i++){
            Type type =types.get(i);
            List<Project> projects = projectMapper.selectProjectByTypeId(type.getId());
            type.setProjects(projects);
        }
        return types;
    }

    @Override
    public PageInfo selectTypeByName(Map map) {
        int pageNum = (int)map.get("pageNum");
        PageHelper.startPage(pageNum,2);
        String name = (String)map.get("name");
        List<Type> types = typeMapper.selectTypeByName(name);
        PageInfo pageInfo=new PageInfo(types);
        return pageInfo;
    }

    @Override
    public Type selectTypeById(long id) {
        return typeMapper.selectTypeById(id);
    }

    @Override
    public int updateType(Type type) {
        return typeMapper.updateType(type);
    }

    @Override
    public int insertType(Type type) {
        return typeMapper.insertType(type);
    }

    @Override
    public int deleteTypeById(long id) {
        return typeMapper.deleteTypeById(id);
    }

    @Override
    public int deleteTypeByProjectId(long projectId) {
        return typeMapper.deleteTypeByProjectId(projectId);
    }

    @Override
    public int insertProjectType(long project, String typeid) {
        return typeMapper.insertProjectType(project,typeid);
    }
}
